package net.xju3_4.dao;
import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDao {

    public PageInfo<UserEntity> pagelist(Integer pageNo, Integer pageSize, UserEntity user) {

        String sql="select " + "id, " + "username, " + "password, " +
                "phone, " +
                "real_name as realName, " +  // 映射到实体类 realName
                "id_card_encrypted as idCardEncrypted, " +  // 映射到实体类 idCardEncrypted
                "driver_license_encrypted as driverLicenseEncrypted, " +  // 映射到实体类 driverLicenseEncrypted
                "avatar_url as avatarUrl, " +// 映射到实体类 avatarUrl
                "status " +
                "from user where ";
                List<Object> params=new ArrayList<>();
                if(user.getUsername()!=null)
                {
                    sql=sql+"username like ? and ";
                    params.add("%"+user.getUsername()+"%");
                }
        if(user.getRealName()!=null)
        {
            sql=sql+"real_name like ?  and  ";
            params.add("%"+user.getRealName()+"%");
        }
                if(user.getPhone()!=null)
                {
                    sql=sql+"phone like ? ";
                    params.add("%"+user.getPhone()+"%");
                }

        sql=sql+"limit ?,?";
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);
        List<UserEntity> users = JdbcUtilsV1.executeQuery(UserEntity.class,sql,params.toArray());
        String sqlCount="select count(*) from user ";
        Long count=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),sqlCount);
        PageInfo<UserEntity> pageInfo = new PageInfo<>(pageNo,pageSize,Math.toIntExact(count),users);
        return pageInfo;
    }

    public int forbid(Integer id) {
        String Sql="update user set status=0 where id=?";
        return JdbcUtilsV1.executeUpdate(JdbcUtilsV1.getConnection(),Sql,id);
    }

    public int activate(Integer id) {
        String Sql="update user set status=1 where id=?";
        return JdbcUtilsV1.executeUpdate(JdbcUtilsV1.getConnection(),Sql,id);
    }

    public int delete(Integer id) {
        String Sql="delete from user where id=?";
        return JdbcUtilsV1.executeUpdate(JdbcUtilsV1.getConnection(),Sql,id);
    }
//统计用户的总数量、激活用户数量、禁用用户数量
    public Map<String, Integer> count() {
        String Sql="select count(*) from user";
        Long totoalUserCount=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),Sql);
        String Sql1="select count(*) from user where status=1";
        Long activeUserCount=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),Sql1);
        String Sql2="select count(*) from user where status=0";
        Long forbidUserCount=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),Sql2);
        if(totoalUserCount==null) totoalUserCount=0L;
        if(activeUserCount==null) activeUserCount=0L;
        if(forbidUserCount==null) forbidUserCount=0L;
        return Map.of("totalUser",Math.toIntExact(totoalUserCount),"activeUser",Math.toIntExact(activeUserCount),"forbidUser",Math.toIntExact(forbidUserCount));
    }
}
